#!/usr/bin/python2.7

import json
import subprocess
import sys


def GetUploadURL(server):
  args = [
    'curl',
    '--silent',
    '%s/api/getReplayUploadURL' % server,
  ]
  output = subprocess.check_output(args)
  parsed = json.loads(output)
  return parsed['url']


def UploadFiles(server, files):
  args = [
    'curl',
    '--silent',
  ]
  for file in files:
    args.extend(['--form', 'file=@%s' % file])
  args.append(GetUploadURL(server))
  output = subprocess.check_output(args)
  parsed = json.loads(output)
  for filename, result in parsed.iteritems():
    print '%s: %s (%s)' % (filename, result['result'], result.get('message', ''))


def chunks(l, n):
  for i in xrange(0, len(l), n):
    yield l[i:i+n]


def main(args):
  server = args[1]
  files = args[2:]
  for chunk in chunks(files, 10):
    UploadFiles(server, chunk)


if __name__ == '__main__':
  sys.exit(main(sys.argv))
